/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "usart.h"
#include "Digital.h"
#include "Code.h"
#include "tim.h"
#include "rtc.h"

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
extern volatile uint32_t countdown_time; 
volatile uint8_t countdown_running = 0; 
uint32_t backupData = 0;
//int32_t encoder_count = 0;

volatile uint8_t current_mode = 0;
RTC_TimeTypeDef sTime; 

/* USER CODE END Variables */
/* Definitions for defaultTask */
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
  .name = "defaultTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for vCountdownTask */
osThreadId_t vCountdownTaskHandle;
const osThreadAttr_t vCountdownTask_attributes = {
  .name = "vCountdownTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for vEncodeTask */
osThreadId_t vEncodeTaskHandle;
const osThreadAttr_t vEncodeTask_attributes = {
  .name = "vEncodeTask",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void *argument);
void Countdown(void *argument);
void Encode(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of defaultTask */
  defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);

  /* creation of vCountdownTask */
  vCountdownTaskHandle = osThreadNew(Countdown, NULL, &vCountdownTask_attributes);

  /* creation of vEncodeTask */
  vEncodeTaskHandle = osThreadNew(Encode, NULL, &vEncodeTask_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void *argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    if(current_mode == 0){
			SevenSegment_Display(countdown_time);
			
		}
		else{
			HAL_RTC_GetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
			SevenSegment_Display(sTime.Hours * 60 + sTime.Minutes);
		}
		
    osDelay(pdMS_TO_TICKS(20));
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_Countdown */
/**
* @brief Function implementing the vCountdownTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Countdown */
void Countdown(void *argument)
{
  /* USER CODE BEGIN Countdown */
  /* Infinite loop */
  for(;;)
  {
		if(countdown_running && countdown_time > 0){
			HAL_Delay(1000);
			countdown_time--;
		}
		else if(countdown_running && countdown_time == 0){
			HAL_Delay(500);
			HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);
			HAL_Delay(1000);
			HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);		
		}
    osDelay(1);
  }
  /* USER CODE END Countdown */
}

/* USER CODE BEGIN Header_Encode */
/**
* @brief Function implementing the vEncodeTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Encode */
void Encode(void *argument)
{
  /* USER CODE BEGIN Encode */
  /* Infinite loop */
  for(;;)
  {
		int16_t encoder_value = Encoder_GetValue();
		if(current_mode == 0 && encoder_value !=0 && countdown_running == 0){
			HAL_TIM_PWM_Stop(&htim4,TIM_CHANNEL_4);
			if((countdown_time % 60) != 0) countdown_time  -= (countdown_time % 60);
			countdown_time += (encoder_value * 60);		
			if(countdown_time < 4294967295 && countdown_time > 4294967176) countdown_time = 3600;
			if(countdown_time > 3600 && countdown_time < 3720) countdown_time = 0;	
			
      HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR2, countdown_time);			
		}
		else if(current_mode && encoder_value != 0){
			sTime.Minutes += encoder_value;
			if(sTime.Minutes == 255) {
				sTime.Minutes = 59;
				sTime.Hours--;
				printf("%d ",sTime.Hours);
			}
			if(sTime.Minutes > 59) 
			{
				sTime.Minutes = 0;
				sTime.Hours++;
			}
			if(sTime.Hours >23){
				sTime.Hours = 0;
			}
			if(sTime.Hours == 255){
				sTime.Hours = 23;
			}

	    backupData = (sTime.Hours << 16) | (sTime.Minutes << 8);
	    HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR1, backupData);
			HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN);
		}
		
		
    osDelay(1);
  }
  /* USER CODE END Encode */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

